package com.yuwubao.uav.tool.service.impl;

import com.yuwubao.framework.bean.PageData;
import com.yuwubao.uav.tool.mapper.DecisionMapper;
import com.yuwubao.uav.tool.mapper.FrontMapper;
import com.yuwubao.uav.tool.service.DecisionService;
import com.yuwubao.uav.tool.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class DecisionServiceImpl implements DecisionService {

    private static final Logger logger = LogManager.getLogger(DecisionServiceImpl.class.getName());

    @Resource
    private DecisionMapper decisionMapper;
    @Resource
    private FrontMapper frontMapper;

    @Override
    public void disableOrEnableDecisionComment(PageData pageData) throws Exception {
        try{
            int userId = 0;
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    decisionMapper.disableOrEnableDecisionComment(pageData);
                    userId = decisionMapper.getCommentUserId(pageData);
                }
            }

            //审核评论通过，给用户增加积分
            if (pageData.get("state") != null && "1".equals(pageData.get("state")) && userId > 0) {
                //获取评论的积分值
                pageData.put("integration",frontMapper.getIntegration1SystemInfo());
                pageData.put("title","评论积分");
                pageData.put("userId",userId);
                //增加用户的积分数
                frontMapper.addUserIntegration(pageData);
                //增加用户的积分记录
                frontMapper.addUserIntegrationHistory(pageData);
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void addDecisionComment(PageData pageData) throws Exception {
        try{
            decisionMapper.addDecisionComment(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void addDecisionQuestion(PageData pageData) throws Exception {
        try{
            decisionMapper.addDecisionQuestion(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public List<PageData> listDecisionComment(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionComment(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listDecisionCommentCount(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionCommentCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }
    @Override
    public List<PageData> listDecisionCommentForBackend(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionCommentForBackend(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listDecisionCommentCountForBackend(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionCommentCountForBackend(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }
    @Override
    public List<PageData> listDecisionQuestion(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionQuestion(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listDecisionQuestionCount(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionQuestionCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }


    @Override
    public void deleteDecision(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    decisionMapper.deleteDecision(pageData);

                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void updateDecision(PageData pageData) throws Exception {
        try{
            decisionMapper.updateDecision(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void disableOrEnableDecision(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    decisionMapper.disableOrEnableDecision(pageData);

                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void disableOrEnableDecisionType(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    decisionMapper.disableOrEnableDecisionType(pageData);

                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void addDecision(PageData pageData) throws Exception {
        try{
            decisionMapper.addDecision(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public List<PageData> listDecision(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecision(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listDecisionCount(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }

    @Override
    public void deleteDecisionType(PageData pageData) throws Exception {
        try{

            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    decisionMapper.deleteDecisionType(pageData);

                }
            }


        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void updateDecisionType(PageData pageData) throws Exception {
        try{
            decisionMapper.updateDecisionType(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public void addDecisionType(PageData pageData) throws Exception {
        try{
            String names = pageData.get("names") == null ? "" : pageData.getString("names");
            if (StringUtil.isNotNvlOrBlank(names)) {
                String[] nameArr = names.split(",");
                if (nameArr != null && nameArr.length>0) {
                    for (String name : nameArr) {
                        pageData.put("name",name);
                        decisionMapper.addDecisionType(pageData);
                    }
                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public List<PageData> listDecisionType(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionType(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public List<PageData> listDecisionTypeAll(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionTypeAll(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listDecisionTypeCount(PageData pageData) throws Exception {
        try{
            return decisionMapper.listDecisionTypeCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }

}
